import 'package:go_router/go_router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../presentation/pages/auth/login_page_web.dart'; // 使用Web专用登录页面
import '../../presentation/pages/auth/register_page.dart';
import '../../presentation/pages/dashboard_page.dart';
import '../../presentation/pages/project/project_list_page.dart';
import '../../presentation/pages/project/project_detail_page.dart';
import '../../presentation/pages/todo/todo_list_page.dart';
import '../../presentation/pages/excel/excel_page.dart';
import '../../presentation/providers/auth_provider_web.dart'; // 使用Web专用认证提供者

/// Web专用应用路由配置（仅支持在线功能）
class AppRouterWeb {
  static final GoRouter router = GoRouter(
    initialLocation: '/login',
    routes: [
      // 认证相关路由
      GoRoute(
        path: '/login',
        name: 'login',
        builder: (context, state) => const LoginPageWeb(), // 使用Web专用登录页面
      ),

      GoRoute(
        path: '/register',
        name: 'register',
        builder: (context, state) => const RegisterPage(),
      ),

      // 主页路由（使用新的仪表板页面）
      GoRoute(
        path: '/dashboard',
        name: 'dashboard',
        builder: (context, state) => const DashboardPage(),
      ),

      // 项目相关路由
      GoRoute(
        path: '/projects',
        name: 'projects',
        builder: (context, state) => const ProjectListPage(),
        routes: [
          GoRoute(
            path: ':projectId', // 修复：移除开头的斜杠
            name: 'project-detail',
            builder: (context, state) {
              final projectId = int.parse(state.pathParameters['projectId']!);
              return ProjectDetailPage(projectId: projectId);
            },
          ),
        ],
      ),

      // 待办任务路由
      GoRoute(
        path: '/todos',
        name: 'todos',
        builder: (context, state) => const TodoListPage(),
      ),

      // Excel功能路由
      GoRoute(
        path: '/excel',
        name: 'excel',
        builder: (context, state) => const ExcelPage(),
      ),
    ],

    // Web端路由重定向逻辑（仅支持在线模式）
    redirect: (context, state) async {
      // 获取认证状态
      final container = ProviderScope.containerOf(context);
      final authState = container.read(authStateProviderWeb); // 使用Web专用认证提供者

      final publicRoutes = ['/login', '/register'];

      // 如果是公开路由，直接通过
      if (publicRoutes.contains(state.fullPath)) {
        return null;
      }

      // 检查用户是否已认证
      if (authState is! AuthSuccessWeb) {
        // 使用Web专用认证状态类型
        // Web端始终需要在线认证，未认证用户重定向到登录页面
        return '/login';
      }

      return null;
    },

    // 错误页面
    errorBuilder: (context, state) => Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(
              Icons.error_outline,
              size: 64,
              color: Colors.red,
            ),
            const SizedBox(height: 16),
            SelectableText(
              '页面未找到',
              style: Theme.of(context).textTheme.headlineSmall,
            ),
            const SizedBox(height: 8),
            SelectableText(
              '路径: ${state.fullPath}',
              style: Theme.of(context).textTheme.bodyMedium,
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () => context.go('/dashboard'),
              child: const Text('返回首页'),
            ),
          ],
        ),
      ),
    ),
  );
}
